Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace JPC.Medialibrary.DAL
    ''' <summary>
    ''' Controller class for Songs
    ''' </summary>
		<System.ComponentModel.DataObject()> Public Partial Class SongController
    
        ' Preload our schema..
        Dim thisSchemaLoad As Song = New Song()
        Private strUserName As String = String.Empty
        Protected ReadOnly Property UserName() As String
            Get
				If strUserName.Length = 0 Then
		        
    				If Not System.Web.HttpContext.Current Is Nothing Then
						strUserName = System.Web.HttpContext.Current.User.Identity.Name
					Else
		        		strUserName = System.Threading.Thread.CurrentPrincipal.Identity.Name
					End If
					Return strUserName
				End If
				Return strUserName
			End Get
        End Property
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchAll() As SongCollection
        
            Dim coll As SongCollection = New SongCollection()
            Dim qry As Query = New Query(Song.Schema)
            coll.LoadAndCloseReader(qry.ExecuteReader())
            Return coll
            
        End Function
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchByID(ByVal SongId As Object) As SongCollection 
        
            Dim coll As SongCollection = New SongCollection().Where("SongId", SongId).Load()
            Return coll
        
        End Function
        
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchByQuery(ByVal qry As SubSonic.Query) As SongCollection 
        
            Dim coll As SongCollection = New SongCollection()
            coll.LoadAndCloseReader(qry.ExecuteReader())
            Return coll
        
        End Function
        <DataObjectMethod(DataObjectMethodType.Delete, True)> Public Function Delete(ByVal SongId As Object) as Boolean
        
            Return (Song.Delete(SongId) = 1)
        
        End Function
        <DataObjectMethod(DataObjectMethodType.Delete, False)> Public Function Destroy(ByVal SongId As Object) as Boolean
        
            Return (Song.Destroy(SongId) = 1)
        
        End Function
        
    	
	    ''' <summary>
	    ''' Inserts a record, can be used with the Object Data Source
	    ''' </summary>
        <DataObjectMethod(DataObjectMethodType.Insert, True)> Public Sub Insert(ByVal AlbumId As Nullable(Of Integer),ByVal ArtistId As Nullable(Of Integer),ByVal Genre As Nullable(Of Short),ByVal Title As String,ByVal Track As Nullable(Of Short),ByVal Year As String,ByVal FileName As String,ByVal LastPlayed As Nullable(Of DateTime),ByVal NumberOfPlays As Nullable(Of Integer))
	   
		    Dim item As Song = New Song()
		    
            item.AlbumId = AlbumId
            
            item.ArtistId = ArtistId
            
            item.Genre = Genre
            
            item.Title = Title
            
            item.Track = Track
            
            item.Year = Year
            
            item.FileName = FileName
            
            item.LastPlayed = LastPlayed
            
            item.NumberOfPlays = NumberOfPlays
            
	    
		    item.Save(UserName)
	   
	   End Sub
    	
	    ''' <summary>
	    ''' Updates a record, can be used with the Object Data Source
	    ''' </summary>
        <DataObjectMethod(DataObjectMethodType.Update, True)> Public Sub Update(ByVal SongId As Integer,ByVal AlbumId As Nullable(Of Integer),ByVal ArtistId As Nullable(Of Integer),ByVal Genre As Nullable(Of Short),ByVal Title As String,ByVal Track As Nullable(Of Short),ByVal Year As String,ByVal FileName As String,ByVal LastPlayed As Nullable(Of DateTime),ByVal NumberOfPlays As Nullable(Of Integer))
	    
		    Dim item As Song = New Song()
	        item.MarkOld()
	        item.IsLoaded = True
		    
			item.SongId = SongId 
				
			item.AlbumId = AlbumId 
				
			item.ArtistId = ArtistId 
				
			item.Genre = Genre 
				
			item.Title = Title 
				
			item.Track = Track 
				
			item.Year = Year 
				
			item.FileName = FileName 
				
			item.LastPlayed = LastPlayed 
				
			item.NumberOfPlays = NumberOfPlays 
				
	        item.Save(UserName)
	    End Sub
    End Class
End Namespace
